
#include "ui_sensor_sht3x_arp.h"
#include "main.h"
#include "bsp_oled_spi.h"
#include <stdio.h>


stSht3xArpValue stSht3xArpVal;

void app_SensorSht3xArpInit(void)
{

    bsp_Sht3xArpInit();


    bsp_OledClearNoRefresh();
    bsp_OledShowChinese(0, 0, 0, 16, 1); //南京至一电子科技
    bsp_OledShowChinese(16, 0, 1, 16, 1); //南京至一电子科技
    bsp_OledShowChinese(32, 0, 2, 16, 1); //南京至一电子科技
    bsp_OledShowChinese(48, 0, 3, 16, 1); //南京至一电子科技
    bsp_OledShowChinese(64, 0, 4, 16, 1); //南京至一电子科技
    bsp_OledShowChinese(80, 0, 5, 16, 1); //南京至一电子科技
    bsp_OledShowChinese(96, 0, 6, 16, 1); //南京至一电子科技
    bsp_OledShowChinese(112, 0, 7, 16, 1); //南京至一电子科技
    //bsp_OledShowString(2, 16, "SHT3XARP_SENSOR", 12,1);
    bsp_OledShowString(12, 18, "SHT3XARP_SENSOR", 12, 1);
    bsp_OledShowString(2, 32, "Temp:", 16, 1);
    bsp_OledShowString(2, 48, "Humi:", 16, 1);
    bsp_OledRefresh();

    bsp_Sht3xArpStartMeasure();

}

void app_SensorSht3xArpDisplay(void)
{
    uint8_t ucKeyCode = KEY_NONE;
    char strBuff[14] = {0};

    app_SensorSht3xArpInit();

    while(gSensorType == SENSOR_SHT3X_ARP)
    {
        /* 实现喂狗,Modbus轮询,刷新UI等操作，不可阻塞 */
        bsp_Idle();

        /* APP运行代码 */
        ucKeyCode = bsp_GetKey();
        if(ucKeyCode != KEY_NONE)
        {
            switch(ucKeyCode)
            {
            case KEY_1_UP:
            {
                bsp_BeepKeyTone();
                gSensorType = SENSOR_SHT2X;
            };
            break;
            case KEY_1_LONG:
            {
                bsp_BeepKeyTone();

            };
            break;
            case KEY_2_UP:
            {
                bsp_BeepKeyTone();
                gSensorType = SENSOR_SHT3X_DIS;
            };
            break;
            case KEY_3_UP:
            {
                //bsp_BeepKeyTone();
                bsp_BeepStart(5, 1, 5); //蜂鸣器响5次，用于按键测试
            };
            break;
            case KEY_3_LONG:   //组合键
            {

            };
            break;
            default:
                break;
            }
        }
        /* USER CODE END WHILE */
        if(bsp_CheckTimer(SOFT_TMR_ID_ONESECOND))
        {
            bsp_LedToggle(1);
            bsp_Sht3xArpMeasure(&stSht3xArpVal);
            memset(strBuff, 0, sizeof(strBuff));
            SEGGER_RTT_SetTerminal(0);
            SEGGER_RTT_printf(0, "SHT3X_ARP SENSOR Data:\n\r");
            if(stSht3xArpVal.iTemp < 0)
            {
                stSht3xArpVal.iTemp = -stSht3xArpVal.iTemp;
                sprintf(strBuff, "-%d.%d C", stSht3xArpVal.iTemp / 10, stSht3xArpVal.iTemp % 10);
            }
            else
            {
                sprintf(strBuff, "%d.%d C", stSht3xArpVal.iTemp / 10, stSht3xArpVal.iTemp % 10);
            }

            /* 通过SEGGER-RTT 打印温度 */
            SEGGER_RTT_printf(0, "TEMP:%s\n\r", strBuff);
            /* 通过串口打印温度 */
            printf("TEMP:%s\n\r", strBuff);
            /* OLED 显示温湿度 */
            bsp_OledShowString(42, 32, strBuff, 16, 1);

            memset(strBuff, 0, sizeof(strBuff));
            sprintf(strBuff, "%d.%d %%RH", stSht3xArpVal.iHumi / 10, stSht3xArpVal.iHumi % 10);
            /* 通过SEGGER-RTT 打印湿度 */
            SEGGER_RTT_printf(0, "HUMI:%s\n\r", strBuff);
            /* 通过串口打印湿度 */
            printf("HUMI:%s\n\r", strBuff);
            /* OLED 显示温湿度 */
            bsp_OledShowString(42, 48, strBuff, 16, 1);
            bsp_OledRefresh();



        }

    }
    bsp_Sht3xArpStopMeasure();
}
